﻿using System.Collections.Generic;
using System.Windows.Media;
using System.Xml.Linq;
using CharacterModel;
using CharacterModel.Extensions;
using System.Linq;
using Framework.Extensions;

namespace MvcApplication1.Views.Portrait
{
    public static class ColorBarHelper
    {
        private static readonly Dictionary<LayerTypeEnum, Color[]> ColorLookup
            = new Dictionary<LayerTypeEnum, Color[]>
                  {
                      {
                          LayerTypeEnum.FaceColor, new[]
                                                       {
                                                           Color.FromRgb(0xd7, 0xc9, 0x82),
                                                           Color.FromRgb(0xc5, 0xa4, 0x64),
                                                           Color.FromRgb(0x62, 0x4d, 0x25),
                                                           Color.FromRgb(0xf1, 0xad, 0x7f),
                                                           Color.FromRgb(0xf6, 0xd3, 0xb2),
                                                           Color.FromRgb(0xdb, 0xd7, 0x60)
                                                       }
                          },
                      {
                          LayerTypeEnum.HairShapeAboveColor, new[]
                                                                 {
                                                                     Color.FromRgb(0x5e, 0x49, 0x23),
                                                                     Color.FromRgb(0xaa, 0, 0),
                                                                     Color.FromRgb(0x9a, 0x5f, 0x24),
                                                                     Color.FromRgb(0x4b, 0xe0, 0x51),
                                                                     Color.FromRgb(0xec, 0xd1, 0x56),
                                                                     Color.FromRgb(0xff, 0xee, 0xc5),
                                                                     Color.FromRgb(0x92, 0x92, 0x92)
                                                                 }
                          },
                      {
                          LayerTypeEnum.HairShapeBelowColor, new[]
                                                                 {
                                                                     Color.FromRgb(0x5e, 0x49, 0x23),
                                                                     Color.FromRgb(0xaa, 0, 0),
                                                                     Color.FromRgb(0x9a, 0x5f, 0x24),
                                                                     Color.FromRgb(0x4b, 0xe0, 0x51),
                                                                     Color.FromRgb(0xec, 0xd1, 0x56),
                                                                     Color.FromRgb(0xff, 0xee, 0xc5),
                                                                     Color.FromRgb(0x92, 0x92, 0x92)
                                                                 }
                          },
                      {
                          LayerTypeEnum.EyesColor, new[]
                                                       {
                                                           Color.FromRgb(0x52, 0xaf, 0xdd),
                                                           Color.FromRgb(0, 0, 0xff),
                                                           Color.FromRgb(0, 0xff, 0x55),
                                                           Color.FromRgb(0xff, 0, 0),
                                                           Color.FromRgb(0x81, 0x40, 0),
                                                           Color.FromRgb(0x1c, 0x0e, 0),
                                                           Color.FromRgb(0, 0, 0xff)
                                                       }
                          },
                      {
                          LayerTypeEnum.MouthLipColor, new[]
                                                       {
                                                           Color.FromRgb(0xff, 0, 0),
                                                           Color.FromRgb(0x70, 0x58, 0x2a),
                                                           Color.FromRgb(0xc6, 0x99, 0x8e)
                                                       }
                          }
                  };

        public static string GetColorBar(LayerTypeEnum layerType)
        {
            if (!ColorLookup.ContainsKey(layerType)) return "";

            return ColorLookup[layerType]
                .Select(color => GetColorButton(color, layerType))
                .SeparateBy("\r\n");
        }

        public static string GetColorButton(Color color, LayerTypeEnum layerType)
        {
            const int size = 50;
            const int strokeWidth = 2;
            const int roundingRadius = 10;

            var div = new XElement("div",
                new XAttribute("style", "margin: 4px; float: left;"));
            var svg = new XElement("svg",
                                   new XAttribute("height", size),
                                   new XAttribute("width", size));
            div.Add(svg);

            var outerRect = new XElement("rect",
                                         new XAttribute("width", size),
                                         new XAttribute("height", size),
                                         new XAttribute("rx", roundingRadius),
                                         new XAttribute("ry", roundingRadius),
                                         new XAttribute("onmouseover", "hilight(this, true)"),
                                         new XAttribute("onmouseout", "hilight(this, false)"),
                                         new XAttribute("style", Colors.Gray.GetSvgStyle()));
            svg.Add(outerRect);

            var onClick = string.Format("changeColor('{0:X2}{1:X2}{2:X2}', {3});", color.R, color.G, color.B, (int)layerType);
            if (layerType == LayerTypeEnum.HairShapeAboveColor)
                onClick += string.Format("changeColor('{0:X2}{1:X2}{2:X2}', {3});", color.R, color.G, color.B, (int)LayerTypeEnum.HairShapeBelowColor);

            var innerRect = new XElement("rect",
                new XAttribute("x", strokeWidth),
                new XAttribute("y", strokeWidth),
                new XAttribute("width", size - strokeWidth * 2),
                new XAttribute("height", size - strokeWidth * 2),
                new XAttribute("rx", roundingRadius),
                new XAttribute("ry", roundingRadius),
                new XAttribute("onmouseover", "hilight(this, true)"),
                new XAttribute("onmouseout", "hilight(this, false)"),
                new XAttribute("onclick", onClick),
                new XAttribute("style", color.GetSvgStyle()));
            svg.Add(innerRect);

            return div.ToString();
        }
    }
}